package utils

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	kafkaMQ "gitee.com/liuxuezhan/ar-platform/base-common/amqp/kafka"
	"gitee.com/liuxuezhan/ar-platform/base-common/amqp/kafka/config"
	"gitee.com/liuxuezhan/ar-platform/base-common/headInfo"
)

// SendKafkaMessage 推送 kafka 消息
func SendKafkaMessage(topic string, ctx context.Context, svcCtx *svc.ServiceContext, msg string) error {
	producer := kafkaMQ.NewProducer(config.ProviderConfig{
		Brokers: svcCtx.Config.KafkaProvider.Brokers,
		Topic:   topic,
	})
	defer producer.Close()
	return producer.ProduceMessage(&kafkaMQ.KafkaMessage{
		Trance: headInfo.GetTrance(ctx),
		Key:    []byte(topic),
		Message: kafkaMQ.Message{
			Data: []byte(msg),
		},
	})
}
